package com.enlorenz.core.channel.activemq.topic;

import java.io.Serializable;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MqMsgListener implements MessageListener{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private MqEventReader mqTopicEventReader;
	
	public MqMsgListener(MqEventReader mqTopicEventReader){
		this.mqTopicEventReader=mqTopicEventReader;
	}
	
	public void onMessage(Message message) {
		if ((message instanceof ObjectMessage)) {
			Serializable eventObj = null;
			try {
				eventObj = ((ObjectMessage) message).getObject();
				mqTopicEventReader.onEvent(eventObj);
			} catch (Exception e) {
				logger.error("处理topic事件出错", e);
				throw new RuntimeException("处理Topic事件出错", e);
			}
		} else if (this.logger
				.isWarnEnabled()) {
			this.logger
					.warn("Topic lisener container is default recive objectMessage but accepted a [{}] msgDetail:{}",
							message.getClass().getSimpleName(),
							message);
		}
	}
	
}
